package cn.com.cym.gamezao.config;

import cn.com.cym.gamezao.util.CommonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import java.io.IOException;

/**
 * 配置项目启动自动启动页面
 */
@Configuration
public class InitBrowserConfig {

    @Value("${server.port}")
    private String port;

    @EventListener({ApplicationReadyEvent.class})
    public void applicationReadyEvent() {

        //如果服务器不是window系统直接返回，不做后续处理
        if(!CommonUtils.isWindows()){
            return;
        }
        System.out.println("应用已经准备就绪 ... 启动浏览器");
        // System.out.println(port);

        port=port==null?"8080":port;

        String url = "http://localhost:"+port+"/api/doc.html";
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
            System.out.println(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
